package com.example.mybatisplus.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.mapper.AdminMapper;
import com.example.mybatisplus.mapper.TeaMapper;
import com.example.mybatisplus.model.domain.Admin;
import com.example.mybatisplus.model.domain.Student;
import com.example.mybatisplus.model.domain.Studif;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.TeaService;
import com.example.mybatisplus.model.domain.Tea;

import java.util.List;


/**
 * 前端控制器
 *
 * @author yp
 * @version v1.0
 * @since 2021-10-22
 */
@Controller
@RequestMapping("/api/tea")
public class TeaController {

    private final Logger logger = LoggerFactory.getLogger(TeaController.class);

    @Autowired
    private TeaService teaService;

    @Autowired
    private TeaMapper teaMapper;

    @Autowired
    private AdminMapper adminMapper;

    /**
     * 描述：根据Id 查询
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id) throws Exception {
        Tea tea = teaService.getById(id);
        return JsonResponse.success(tea);
    }

    /**
     * 描述：根据Id删除
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        teaService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
     * 描述：根据Id 更新
     */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateTea(Tea tea) throws Exception {
        teaService.updateById(tea);
        return JsonResponse.success(null);
    }


    /**
     * 描述:创建Tea
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(Tea tea) throws Exception {
        teaService.save(tea);
        return JsonResponse.success(null);
    }


    /**
     * 个人信息的查询
     *
     * @return
     */
    @PostMapping("/Teach")
    @ResponseBody
    public JsonResponse Teach() {
        Tea tea = teaMapper.selectByNumber1(SessionUtils.getCurrentUserInfo().getLoginName());
        return JsonResponse.success(tea);
    }


    /**
     * 个人信息的修改
     */
    @PostMapping("/teaChange")
    @ResponseBody
    public JsonResponse teaChange(@RequestBody Tea tea) {
        teaService.updateById(tea);
        return JsonResponse.success(tea);
    }


    /**
     * 学院端查询教师信息
     */
    @GetMapping("/teaList")
    @ResponseBody
    public JsonResponse getpageTea(@RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                   @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        Page<Tea> page = new Page<>(pageNo, pageSize);
        page = teaService.page(page);
        return JsonResponse.success(page);
    }

    /**
     * @param
     * @return 学院端教师信息修改
     */
    @PostMapping("/updatatea")
    @ResponseBody
    public JsonResponse updataTea(@RequestBody Tea tea) {
        teaMapper.updateByTeaNumber(tea.getTeaNumber(), tea);
        return JsonResponse.success(null);
    }

    /**
     * @param tea
     * @return 学院端教师信息删除
     */
    @PostMapping("/deletetea")
    @ResponseBody
    public JsonResponse deleteStu(@RequestBody Tea tea) {
        teaMapper.deleteByTeaNumber(tea.getTeaNumber());
        adminMapper.deleteByTeaNumber(tea.getTeaNumber());
        return JsonResponse.success(null);
    }

    /**
     * 学院端对教师得模糊查询
     */
    @PostMapping("/selectionWithTea")
    @ResponseBody
    public JsonResponse selectionWithTea(@RequestBody Tea tea) {
        List<Tea> teas = teaService.selectByTea(tea);
        return JsonResponse.success(teas);
    }
    /**
     *学院端对教师得添加
     */
    @PostMapping("/insertTea")
    @ResponseBody
    public JsonResponse insertStudent(@RequestBody Tea tea){
        Admin admin=new Admin();
        admin.setLoginName(tea.getTeaNumber());
        admin.setName(tea.getTeaName());
        admin.setRemark(2L);
        admin.setPassword(tea.getTeaNumber().substring(tea.getTeaNumber().length()-6));
        adminMapper.insert(admin);
        Admin admin1 = adminMapper.selectName(admin.getLoginName());
        teaMapper.insert(tea);
        return  JsonResponse.success(null);
    }
}

